package com.yanqu.road.server.logic.thread;

import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.utils.executor.GeneralTask;
import com.yanqu.road.utils.http.HttpHelper;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

public class CouponReportTask extends GeneralTask {
    private Logger logger = LogManager.getLogger(CouponReportTask.class.getName());

    private long userID;                      //角色id
    private String platformUserId;              //平台账户id
    private int couponGoodsId;                  //代金券道具id
    private String couponGoodsName;             //代金券名称
    private int isGet;                          //是否获得
    private long couponItemTotalCount;          //代金券总数
    private long couponItemCount;               //代金券当前数量
    private String triggerType;                 //触发类型
    private String triggerWay;                  //触发途径
    private int titleId;                        //身份等级
    private int potential;                      //潜力分
    private Date triggerTime;                   //触发时间
    private int mallId;                         //商品id
    private String mallName;                    //商品名称
    private String price;                       //实付金额
    private String useWay;                      //消耗途径
    private Date useTime;                       //使用时间

    public CouponReportTask(long userID, String platformUserId, int couponGoodsId, String couponGoodsName, int isGet, long couponItemTotalCount,
                            long couponItemCount, String triggerType, String triggerWay, int titleId, int potential, Date triggerTime, int mallId,
                            String mallName, String price, String useWay, Date useTime) {
        this.userID = userID;
        this.platformUserId = platformUserId;
        this.couponGoodsId = couponGoodsId;
        this.couponGoodsName = couponGoodsName;
        this.isGet = isGet;
        this.couponItemTotalCount = couponItemTotalCount;
        this.couponItemCount = couponItemCount;
        this.triggerType = triggerType;
        this.triggerWay = triggerWay;
        this.titleId = titleId;
        this.potential = potential;
        this.triggerTime = triggerTime;
        this.mallId = mallId;
        this.mallName = mallName;
        this.price = price;
        this.useWay = useWay;
        this.useTime = useTime;
    }

    @Override
    public void doRun() {
        String url = ChannelConfig.COUPON_REPORT_URL;
        if(StringUtils.isNullOrEmpty(url)){
            return;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SortedMap<String, String> paramMap = new TreeMap<>();
        paramMap.put("apikey", ChannelConfig.COUPON_REPORT_URL_API_KEY);
        paramMap.put("USER_ID", userID + "");
        paramMap.put("UID", platformUserId);
        paramMap.put("VOUCHER_ID", couponGoodsId + "");
        paramMap.put("VOUCHER_NAME", couponGoodsName);
        paramMap.put("USED", isGet + "");
        paramMap.put("NUM", couponItemTotalCount + "");
        paramMap.put("C_NUM", couponItemCount + "");
        paramMap.put("TRIGGER_TYPE", triggerType);
        paramMap.put("TRIGGER_WAY", triggerWay);
        paramMap.put("LEVEL", titleId + "");
        paramMap.put("POTENTIAL", potential + "");
        paramMap.put("TRIGGER_TIME", dateFormat.format(triggerTime));
        paramMap.put("GIFT_ID", mallId + "");
        paramMap.put("GIFT_NAME", mallName);
        paramMap.put("MONEY", price);
        paramMap.put("USED_WAY", useWay);
        paramMap.put("USED_TIME", dateFormat.format(useTime));
        try {
            Map<String, String> resultMap = HttpHelper.doGet(url, paramMap);
            if(null == resultMap){
                logger.info("CouponReport request error");
            }else {
                logger.info("CouponReport code:{}; result:{}", resultMap.get("code"), resultMap.get("body"));
            }
        } catch (Exception e) {
            logger.error("CouponReport error:",e);
        }
    }
}
